/****************************************************************************
 ** $Filename: neuronWTA.cpp
 ** $Header$
 **
 ** $Author: Mark Blanchard
 ** 
 ** $CreateDate: Tue Dec 09 23:42:08 2001
 ** $Date$
 **
 ** $Keywords:
 ** $Description:
 **
 *****************************************************************************/

#include "neuronWTA.hpp"


// Interface for dynamic loading is built using a macro.
MAKE_NEURON_DLL_INTERFACE(iqrcommon::ClsNeuronWTA, 
			  "Soft Winner Take All")

iqrcommon::ClsNeuronWTA::ClsNeuronWTA()
    : ClsNeuron(),
      pVmembrane(0),
      pActivity(0)
{    
    pExcGain = addDoubleParameter("excGain", "Excitatory gain",
				  1.0, 0.0, 10.0, 4, 
				  "Gain of excitatory inputs.\n"
				  "The inputs are summed before\n"
				  "being multiplied by this gain.",
				  "Input");
    
    pInhGain = addDoubleParameter("inhGain", "Inhibitory gain",
				  1.0, 0.0, 10.0, 4, 
				  "Gain of inhibitory inputs.\n"
				  "The inputs are summed before\n"
				  "being multiplied by this gain.",
				  "Input");

    // This neuron type doesn't use modulatory input, so don't include
    // ModGain.

    // Membrane persistence.
   pThreshold = addDoubleParameter("threshold", "Threshold potential",
				    0.0, 0.0, 1.0, 4, 
				    "Membrane potential threshold for output activity.",
				    "Membrane");   
    // Add state variables.
    pVmembrane = addStateVariable("vm", "Membrane potential");
    pActivity  = addOutputState("act", "Activity");
    
   
}
void iqrcommon::ClsNeuronWTA::reset(){
 
}
void iqrcommon::ClsNeuronWTA::initialize(){
 
}

void iqrcommon::ClsNeuronWTA::update()
{

    
    // Get local references to the state arrays.
    StateArray &excitation = getExcitatoryInput();
    StateArray &inhibition = getInhibitoryInput();
    
    StateArray &vm         = pVmembrane->getStateArray();
    StateArray &activity   = pActivity->getStateArray();
    
    // Get parameter values from parameter objects.
    double excGain     = pExcGain->getValue();
    double inhGain     = pInhGain->getValue();
   
    
    double threshold   = pThreshold->getValue();
   
    
    
    
    excitation[0] *= excGain;
    inhibition[0] *= inhGain;
    
    // Calculate membrane potential
    vm[0] = excitation[0];// see 1 * excGain;
    vm[0] -= inhibition[0];// see 1 * inhGain;
    
    activity[0]=0;
    
    vm[0][vm[0] <  threshold]  = 0.0;
   
    int max=0;
    if(vm[0].sum()>0){
      for (uint i=0;i<vm[0].size();++i){
	if(vm[0][i]>vm[0][max])
	  max=i;
      }  
      activity[0][max]=vm[0][max];
    }
}
